// packageAPI/pages/media/background-audio/background-audio.js
Page({
  data: {
    title: '',
    zh: '',
    webUrl: '',
    schema: {
      groups: []
    },
    formData: {},
    result: '',
    apiName: "",
    // 背景音频管理器
    backgroundAudioManager: null,
    // 播放状态
    playing: false,
    // 暂停状态
    paused: false,
    // 播放时间
    currentTime: 0,
    // 总时长
    duration: 0,
    // 音频源
    audioSrc: 'https://dldir1.qq.com/music/release/upload/t_mm_file_publish/2339610.m4a'
  },

  onLoad: function (options) {
    console.log('Background Audio API页面加载');
    console.log('路由传递的参数:', options);
    
    // 处理路由参数
    if (options) {
      const routeData = {
        zh: options.zh,            
        title: options.title,       
        remark: options.remark,
        webUrl: options.webUrl    
      };
      console.log(routeData,888888)
      this.setData({  
        title: routeData.title,       
        zh: routeData.zh,
        webUrl: routeData.webUrl,
        result: `页面加载完成 - ${routeData.title}`
      });
      console.log(this.data.routeData,99999)
    } else {
      this.setData({
        title: 'Background Audio API',                           
        zh: 'Background Audio API',
        webUrl: '',
        apiName: 'Background Audio API',
        result: '页面加载完成 - 使用默认配置'
      });
    }

    // 初始化背景音频管理器
    this.initBackgroundAudioManager();
  },

  // API处理函数
  handleApiCall: function (e) {
    const apiName = e.currentTarget.dataset.apiname;

    this.setData({
      apiName: apiName
    });

    switch (apiName) {
      case 'wx.stopBackgroundAudio':
        this.stopBackgroundAudioFunc();
        break;
      case 'wx.seekBackgroundAudio':
        this.seekBackgroundAudioFunc();
        break;
      case 'wx.playBackgroundAudio':
        this.playBackgroundAudioFunc();
        break;
      case 'wx.pauseBackgroundAudio':
        this.pauseBackgroundAudioFunc();
        break;
      case 'wx.onBackgroundAudioStop':
        this.onBackgroundAudioStopFunc();
        break;
      case 'wx.onBackgroundAudioPlay':
        this.onBackgroundAudioPlayFunc();
        break;
      case 'wx.onBackgroundAudioPause':
        this.onBackgroundAudioPauseFunc();
        break;
      case 'wx.getBackgroundAudioPlayerState':
        this.getBackgroundAudioPlayerStateFunc();
        break;
      case 'wx.getBackgroundAudioManager':
        this.getBackgroundAudioManagerFunc();
        break;
      case 'BackgroundAudioManager.onCanplay':
        this.onCanplayFunc();
        break;
      case 'BackgroundAudioManager.onEnded':
        this.onEndedFunc();
        break;
      case 'BackgroundAudioManager.onError':
        this.onErrorFunc();
        break;
      case 'BackgroundAudioManager.onNext':
        this.onNextFunc();
        break;
      case 'BackgroundAudioManager.onPause':
        this.onPauseFunc();
        break;
      case 'BackgroundAudioManager.onPlay':
        this.onPlayFunc();
        break;
      case 'BackgroundAudioManager.onPrev':
        this.onPrevFunc();
        break;
      case 'BackgroundAudioManager.onSeeked':
        this.onSeekedFunc();
        break;
      case 'BackgroundAudioManager.onSeeking':
        this.onSeekingFunc();
        break;
      case 'BackgroundAudioManager.onStop':
        this.onStopFunc();
        break;
      case 'BackgroundAudioManager.onTimeUpdate':
        this.onTimeUpdateFunc();
        break;
      case 'BackgroundAudioManager.onWaiting':
        this.onWaitingFunc();
        break;
      case 'BackgroundAudioManager.pause':
        this.pauseFunc();
        break;
      case 'BackgroundAudioManager.play':
        this.playFunc();
        break;
      case 'BackgroundAudioManager.seek':
        this.seekFunc();
        break;
      case 'BackgroundAudioManager.stop':
        this.stopFunc();
        break;
      default:
        this.showResult(`未知的API: ${apiName}`);
    }
  },

  // 初始化背景音频管理器
  initBackgroundAudioManager: function () {
    try {
      this.backgroundAudioManager = wx.getBackgroundAudioManager();
      
      // 设置音频信息
      this.backgroundAudioManager.title = '此时此刻';
      this.backgroundAudioManager.epname = '此时此刻';
      this.backgroundAudioManager.singer = '许巍';
      this.backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';
      
      this.showResult({
        success: true,
        message: '背景音频管理器初始化成功',
        manager: !!this.backgroundAudioManager
      });
    } catch (error) {
      console.error('背景音频管理器初始化失败:', error);
      this.showResult({
        success: false,
        message: '背景音频管理器初始化失败',
        error: error
      });
    }
  },

  // wx.stopBackgroundAudio
  stopBackgroundAudioFunc: function () {
    try {
      wx.stopBackgroundAudio({
        success: (res) => {
          this.setData({
            playing: false,
            paused: false
          });
          this.showResult({
            result: res
          });
        },
        fail: (err) => {
          this.showResult({
            success: false,
            message: '停止背景音频失败',
            error: err
          });
        }
      });
    } catch (error) {
      this.showResult({
        success: false,
        message: '停止背景音频异常',
        error: error
      });
    }
  },

  // wx.seekBackgroundAudio
  seekBackgroundAudioFunc: function () {
    try {
      wx.seekBackgroundAudio({
        position: 30, // 跳转到30秒位置
        success: (res) => {
          this.showResult({
            result: res,
            position: 30
          });
        },
        fail: (err) => {
          this.showResult({
            error: err
          });
        }
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.playBackgroundAudio
  playBackgroundAudioFunc: function () {
    try {
      wx.playBackgroundAudio({
        dataUrl: this.data.audioSrc,
        title: '此时此刻',
        success: (res) => {
          this.setData({
            playing: true,
            paused: false
          });
        
          this.showResult({
            result: res,
            audioSrc: this.data.audioSrc
          });
        },
        fail: (err) => {
          this.showResult({
            
            error: err
          });
        }
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.pauseBackgroundAudio
  pauseBackgroundAudioFunc: function () {
    try {
      wx.pauseBackgroundAudio({
        success: (res) => {
          this.setData({
            playing: false,
            paused: true
          });
          this.showResult({
            result: res
          });
        },
        fail: (err) => {
          this.showResult({
            error: err
          });
        }
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.onBackgroundAudioStop
  onBackgroundAudioStopFunc: function () {
    try {
      wx.onBackgroundAudioStop((res) => {
        this.setData({
          playing: false,
          paused: false
        });
        this.showResult({
          event: 'onBackgroundAudioStop',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        success: false,
        message: '背景音频停止事件监听异常',
        error: error
      });
    }
  },

  // wx.onBackgroundAudioPlay
  onBackgroundAudioPlayFunc: function () {
    try {
      wx.onBackgroundAudioPlay((res) => {
        this.setData({
          playing: true,
          paused: false
        });
        this.showResult({
          event: 'onBackgroundAudioPlay',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.onBackgroundAudioPause
  onBackgroundAudioPauseFunc: function () {
    try {
      wx.onBackgroundAudioPause((res) => {
        this.setData({
          playing: false,
          paused: true
        });
        this.showResult({
          event: 'onBackgroundAudioPause',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.getBackgroundAudioPlayerState
  getBackgroundAudioPlayerStateFunc: function () {
    try {
      wx.getBackgroundAudioPlayerState({
        success: (res) => {
          this.showResult({
            result: res
          });
        },
        fail: (err) => {
          this.showResult({
            error: err
          });
        }
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // wx.getBackgroundAudioManager
  getBackgroundAudioManagerFunc: function () {
    try {
      const manager = wx.getBackgroundAudioManager();
      this.showResult({
        
        manager: !!manager,
        managerInfo: {
          title: manager.title,
          singer: manager.singer,
          coverImgUrl: manager.coverImgUrl,
          src: manager.src,
          duration: manager.duration,
          currentTime: manager.currentTime,
          paused: manager.paused
        }
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.onCanplay
  onCanplayFunc: function () {
    try {
      this.backgroundAudioManager.onCanplay((res) => {
        this.showResult({
          event: 'onCanplay',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onEnded
  onEndedFunc: function () {
    try {
      this.backgroundAudioManager.onEnded((res) => {
        this.setData({
          playing: false,
          paused: false
        });
        this.showResult({
          event: 'onEnded',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onError
  onErrorFunc: function () {
    try {
      this.backgroundAudioManager.onError((res) => {
        this.showResult({
          event: 'onError',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onNext
  onNextFunc: function () {
    try {
      this.backgroundAudioManager.onNext((res) => {
        this.showResult({
          event: 'onNext',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onPause
  onPauseFunc: function () {
    try {
      this.backgroundAudioManager.onPause((res) => {
        this.setData({
          playing: false,
          paused: true
        });
        this.showResult({
          event: 'onPause',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onPlay
  onPlayFunc: function () {
    try {
      this.backgroundAudioManager.onPlay((res) => {
        this.setData({
          playing: true,
          paused: false
        });
        this.showResult({
          event: 'onPlay',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onPrev
  onPrevFunc: function () {
    try {
      this.backgroundAudioManager.onPrev((res) => {
        this.showResult({
          event: 'onPrev',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onSeeked
  onSeekedFunc: function () {
    try {
      this.backgroundAudioManager.onSeeked((res) => {
        this.showResult({
          event: 'onSeeked',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onSeeking
  onSeekingFunc: function () {
    try {
      this.backgroundAudioManager.onSeeking((res) => {
        this.showResult({
          event: 'onSeeking',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onStop
  onStopFunc: function () {
    try {
      this.backgroundAudioManager.onStop((res) => {
        this.setData({
          playing: false,
          paused: false
        });
        this.showResult({
          event: 'onStop',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // BackgroundAudioManager.onTimeUpdate
  onTimeUpdateFunc: function () {
    try {
      this.backgroundAudioManager.onTimeUpdate((res) => {
        this.setData({
          currentTime: res.currentTime || 0,
          duration: res.duration || 0
        });
        this.showResult({
          
          event: 'onTimeUpdate',
          result: res,
          currentTime: res.currentTime,
          duration: res.duration
        });
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.onWaiting
  onWaitingFunc: function () {
    try {
      this.backgroundAudioManager.onWaiting((res) => {
        this.showResult({
          
          event: 'onWaiting',
          result: res
        });
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.pause
  pauseFunc: function () {
    try {
      this.backgroundAudioManager.pause();
      this.setData({
        playing: false,
        paused: true
      });
      this.showResult({
        
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.play
  playFunc: function () {
    try {
      this.backgroundAudioManager.src = this.data.audioSrc;
      this.backgroundAudioManager.play();
      this.setData({
        playing: true,
        paused: false
      });
      this.showResult({
        
        audioSrc: this.data.audioSrc
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.seek
  seekFunc: function () {
    try {
      this.backgroundAudioManager.seek(30); // 跳转到30秒
      this.showResult({
        
        position: 30
      });
    } catch (error) {
      this.showResult({
        
        error: error
      });
    }
  },

  // BackgroundAudioManager.stop
  stopFunc: function () {
    try {
      this.backgroundAudioManager.stop();
      this.setData({
        playing: false,
        paused: false
      });
      this.showResult({
        
      });
    } catch (error) {
      this.showResult({
        error: error
      });
    }
  },

  // 显示操作结果
  showResult: function (message) {
    this.setData({
      result: message
    });
  },

  // 表单数据变化事件
  getFormData: function (e) {
    this.setData({
      formData: e.detail
    });
  }
})
